﻿Imports System.Text

Public Class frmBsiteResult
    Protected m_cBsiteMess As New clsDbBsiteMess
    Private szResult As String
    Private szIP As String
    Public outHeader As InSiteWebService.OutHeader  ' bSite応答

    Public Const HOLD_STRING1 = "FREZ"
    Public Const HOLD_STRING2 = "HOLD"
    Public htMsgMst As New Hashtable

    ''' <summary>
    ''' 結果フォーム初期化
    ''' </summary>
    ''' <param name="IP">自機IPアドレス</param>
    ''' <param name="szRslt">処理結果（OK/NG）</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal IP As String, ByVal szRslt As String)

        ' この呼び出しはデザイナーで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        szResult = szRslt
        szIP = IP

        outHeader = Nothing

        btnResultOK.DialogResult = Windows.Forms.DialogResult.OK
        btnForce.DialogResult = Windows.Forms.DialogResult.Retry
    End Sub

    'フォームロード処理
    Private Sub frmBsiteResult_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' b-Site送信対応変更　2017.6.20
        'Dim dtRslt As DataTable = m_cBsiteMess.GetErrorList(szIP)
        Dim dtRslt As DataTable = New DataTable
        dtRslt.Columns.Add(htMsgMst("MSG00128"))
        dtRslt.Columns.Add(htMsgMst("MSG00129"))
        If outHeader IsNot Nothing AndAlso
            outHeader.ResultCode IsNot Nothing Then
            For i = 0 To outHeader.ResultCode.Count - 1
                If i > outHeader.ResultMessage.Count - 1 Then
                    Exit For
                End If
                Dim drRslt As DataRow = dtRslt.NewRow
                drRslt(0) = outHeader.ResultCode(i)
                drRslt(1) = outHeader.ResultMessage(i)
                dtRslt.Rows.Add(drRslt)
            Next
        End If
        ' b-Site送信対応変更 End
        dgvRsltCode.DataSource = dtRslt

        Select Case szResult
            Case OK
                lblRslt.Text = String.Format(htMsgMst("MSG00127"), OK)
                dgvRsltCode.BackgroundColor = Color.Lime
                Me.BackColor = Color.Lime
                btnForce.Visible = False
                TimerRslt.Start()
            Case WA
                lblRslt.Text = String.Format(htMsgMst("MSG00127"), WA)
                dgvRsltCode.BackgroundColor = Color.Yellow
                Me.BackColor = Color.Yellow
                btnForce.Visible = True
            Case NG
                lblRslt.Text = String.Format(htMsgMst("MSG00127"), NG)
                dgvRsltCode.BackgroundColor = Color.Red
                Me.BackColor = Color.Red
                btnForce.Visible = False
        End Select
    End Sub

    Private Sub frmBsiteResult_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        For Each row As DataGridViewRow In dgvRsltCode.Rows
            If row.Cells(0).Value.ToString.StartsWith(HOLD_STRING1) OrElse _
               row.Cells(0).Value.ToString.StartsWith(HOLD_STRING2) Then
                Me.DialogResult = Windows.Forms.DialogResult.Abort
            End If
        Next
    End Sub

    '******************************************************************************
    '*  「エラーコード」テーブル処理
    '******************************************************************************
    Private Sub dgvErrCode_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles dgvRsltCode.Paint
        For Each row As DataGridViewRow In dgvRsltCode.Rows
            row.Height = 36
        Next
    End Sub

    Protected Sub dgvErrCode_DataSourceChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvRsltCode.DataSourceChanged
        Dim dgv As DataGridView = CType(sender, DataGridView)

        dgv.Columns(0).Width = 160
        dgv.Columns(1).Width = 488
        dgv.EnableHeadersVisualStyles = False
        For Each row As DataGridViewRow In dgv.Rows
            Try
                Dim szCode As String = row.Cells(1).Value.ToString
                Dim i As Integer
                Dim iLength As Integer = szCode.Length
                Dim lstByte As New List(Of Byte)
                For i = 0 To iLength - 2 Step 2
                    lstByte.Add(CInt("&H" & szCode.Substring(i, 2)))
                Next
                row.Cells(1).Value = Encoding.UTF8.GetString(lstByte.ToArray)
            Catch
            End Try
        Next
        dgv.ClearSelection()
    End Sub


    Private Sub TimerRslt_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerRslt.Tick
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    'Private Sub btnResultOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResultOK.Click
    '    Me.Close()
    'End Sub

    ''強制完成
    'Private Sub btnForce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForce.Click

    '    Me.Close()
    'End Sub


    '>>20180116 佐川
    Public Property szResultData() As String
        Get
            szResultData = szResult
        End Get
        Set(ByVal value As String)
            szResult = value
        End Set
    End Property
    '>>20180116 佐川
End Class